set(CMAKE_REQUIRED_LIBRARIES_OLD ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES_OLD ${CMAKE_REQUIRED_INCLUDES})
check_library_exists(SDL2 SDL_GL_CreateContext "" HAVE_SDL2_LIB)
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_OLD})
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_OLD})
if(HAVE_SDL2_LIB)
  foreach(tgt sdlplay )
    add_executable(${tgt} ${APPTYPE} ${tgt}.cpp)
    target_link_libraries(${tgt} PRIVATE mdk SDL2main SDL2)
    if(NOT WIN32)
      target_link_libraries(${tgt} PRIVATE dl)
    endif()
    if(WIN32)
      target_link_libraries(${tgt} PRIVATE winmm imm32 ole32 version) # for static sdl
    elseif(APPLE)
      target_link_libraries(${tgt} PRIVATE iconv "-framework CoreFoundation" "-framework CoreGraphics" "-framework Foundation" "-framework AppKit" "-framework IOKit" "-framework AudioToolbox" "-framework CoreAudio" "-framework ForceFeedback" "-framework Metal" "-framework CoreVideo" "-framework CoreServices" "-framework Carbon"
      "-weak_framework CoreHaptics" "-weak_framework GameController"
      ) # for static sdl
    endif()
    if(EXISTS ${CMAKE_SOURCE_DIR}/external/include/SDL2)
      target_include_directories(${tgt} PRIVATE ${CMAKE_SOURCE_DIR}/external/include/SDL2)
    elseif(EXISTS /usr/local/include/SDL2)
      target_include_directories(${tgt} PRIVATE /usr/local/include/SDL2)
    elseif(EXISTS /opt/homebrew/include/SDL2)
      target_include_directories(${tgt} PRIVATE /opt/homebrew/include/SDL2)
    elseif(EXISTS /usr/include/SDL2)
      target_include_directories(${tgt} PRIVATE /usr/include/SDL2)
    endif()
    install(TARGETS ${tgt} RUNTIME DESTINATION bin)
  endforeach()
endif()